package com.icez.like_star_library

import android.animation.TypeEvaluator
import android.graphics.PointF
import android.util.Log
import kotlin.math.pow
import kotlin.math.sqrt

/**
 * 贝塞尔估值器
 * @sample
 * @author Icez
 */
class BezierEvaluator(private val middlePointF1: PointF?, private val middlePointF2: PointF?) :
    TypeEvaluator<PointF> {

    override fun evaluate(time: Float, start: PointF?, end: PointF?): PointF {
        val pointF = PointF()

        val t = 1 - time
        val startX = start?.x?:0f
        val startY = start?.y?:0f
        val endX = end?.x?:0f
        val endY = end?.y?:0f


        if (middlePointF1 == null && middlePointF2 != null) {//二阶曲线
            pointF.x = startX * t.pow(2) + 2 * middlePointF2.x * time * t + time.pow(2) * endX
            pointF.y = startY * t.pow(2) + 2 * middlePointF2.y * time * t + time.pow(2) * endY
        } else if (middlePointF1 != null && middlePointF2 == null) {//二阶曲线
            pointF.x = startX * t.pow(2) + 2 * middlePointF1.x * time * t + time.pow(2) * endX
            pointF.y = startY * t.pow(2) + 2 * middlePointF1.y * time * t + time.pow(2) * endY
        } else if (middlePointF1 != null && middlePointF2 != null) {//三阶曲线
            pointF.x = t.pow(3) * startX + 3 * middlePointF1.x * time * t.pow(2) +
                    3 * middlePointF2.x * time.pow(2) * t + endX * time.pow(3)
            pointF.y = t.pow(3) * startY + 3 * middlePointF1.y * time * t.pow(2) +
                    3 * middlePointF2.y * time.pow(2) * t + endY * time.pow(3)
        }else{//一阶曲线
            pointF.x = startX * t + endX * time
            pointF.y = startY * t + endY * time
        }
        return pointF
    }
}